package cn.xfdzx.android.apps.shop.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDShopDetailsActivity;
import cn.xfdzx.android.apps.shop.adapter.HomeMarketNearShopAdapter;
import cn.xfdzx.android.apps.shop.base.MvpBaseFragment;
import cn.xfdzx.android.apps.shop.evenbus.MarketNearShopSelectMessage;
import cn.xfdzx.android.apps.shop.fragment.home.contract.MarketShopNearContract;
import cn.xfdzx.android.apps.shop.fragment.home.presenter.MarketShopNearPresenter;
import cn.xfdzx.android.apps.shop.fragment.home.request.NearShopRequest;
import cn.xfdzx.android.apps.shop.fragment.home.response.HomeNearShop;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.ToastUtil;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;

/**
 * "
 * 超市批发附近店铺
 */

public class MarketNearShopFagment extends MvpBaseFragment<MarketShopNearPresenter> implements MarketShopNearContract.View {

    @BindView(R.id.recycle_shop)
    RecyclerView recycleShop;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private HomeMarketNearShopAdapter adapter;

    int page =1;
    private String desc;

    public static MarketNearShopFagment newInstance(String param1) {
        MarketNearShopFagment fragment = new MarketNearShopFagment();
        Bundle args = new Bundle();
        args.putString("id", param1);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public MarketShopNearPresenter createPresenter() {
        return new MarketShopNearPresenter();
    }

    @Override
    protected void initView(View view) {
        EventBus.getDefault().register(this);

        initData("");
        recycleShop.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new HomeMarketNearShopAdapter();
        recycleShop.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), XFDShopDetailsActivity.class);
                intent.putExtra("shop_id",list.get(position).getId()+"" );
                getActivity().startActivity(intent);
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                page++;
                initData(desc);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_market_near_shop_fagment;
    }


    @Override
    public void onError(Throwable throwable) {

    }


    @Subscribe(threadMode = ThreadMode.POSTING)
    public void getHomeNearSel(MarketNearShopSelectMessage messageEvent) {

        page=1;
        refreshLayout.setNoMoreData(false);
        if (messageEvent.getMessage() == 1) {
            initData("");
             desc = "";

        } else if (messageEvent.getMessage() == 2) {
            initData("desc");
            desc = "desc";


        }

    }

    /**
     *   初始化 附近门店的数据
     * @param des
     */
    private void initData(String des) {
        NearShopRequest shopRequest = new NearShopRequest();
        shopRequest.setSid("0");
        shopRequest.setIs_store("1");
        shopRequest.setIndex_status("1");
        shopRequest.setSalesOrder(des);
        shopRequest.setPages(page);
        shopRequest.setLimit(10);

        mPresenter.getNearShop(shopRequest);
    }

    List<HomeNearShop.DataBean> list = new ArrayList<>();


    @Override
    public void onSuceessData(HomeNearShop homeNearShop) {
        if (page == 1) {
            list.clear();
        }
        list.addAll(homeNearShop.getData());
        adapter.setNewData(list);


        if (homeNearShop.getData().size() == 0) {
            refreshLayout.finishLoadMoreWithNoMoreData();
        }

        refreshLayout.finishLoadMore(true);
    }

    @Override
    public void onFail(String reason) {
        refreshLayout.finishLoadMore(true);

    }
}
